//
//  UICollectionView+CYBaseRefreshInit.m
//  SafetyInHand
//
//  Created by admin on 2022/7/27.
//

#import "UIScrollView+CYBaseRefreshInit.h"

// 模块内置关联文件
#import "CYProxy.h"

// 依赖文件
#import "MJRefresh.h"

@interface UIScrollView ()
@property (nonatomic, weak) id <CYRefreshLoadMoreProtocol> cy_delegate;
@property (nonatomic, assign) BOOL isHiddenLoadAllDataLabel;
@end


static NSString *LIST_VIEW_IS_HIDDEN_LOAD_ALL_DATA_LABEL_KEY = @"LIST_VIEW_IS_HIDDEN_LOAD_ALL_DATA_LABEL_KEY";

@implementation UIScrollView (CYCollectionViewInitBase)
- (void)initDefaultListViewWithDelegate:(id <CYRefreshLoadMoreProtocol>)delegate {
    [self initDefaultListViewWithDelegate:delegate isLoadMore:NO];
}

- (void)initDefaultListViewWithDelegate:(id <CYRefreshLoadMoreProtocol>)delegate
                             isLoadMore:(BOOL)isLoadMore {

    self.cy_delegate = delegate;

    __weak __typeof(self) WS = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (WS.cy_delegate && [WS.cy_delegate respondsToSelector:@selector(cy_listViewDidRefresh)]) {
            [WS.cy_delegate cy_listViewDidRefresh];
        }
    }];

    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.font = [UIFont systemFontOfSize:13];

    self.mj_header = header;

    if (isLoadMore) {
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            if (WS.cy_delegate && [WS.cy_delegate respondsToSelector:@selector(cy_listViewLoadMore)]) {
                [WS.cy_delegate cy_listViewLoadMore];
            }
        }];
        footer.stateLabel.font = [UIFont systemFontOfSize:13];
        footer.stateLabel.textColor = [UIColor colorWithRed:144.0 / 255
                                                      green:144.0 / 255
                                                       blue:144.0 / 255
                                                      alpha:1];
        [footer setTitle:@"--- 到底了 ---" forState:MJRefreshStateNoMoreData];
        CGRect rect = footer.frame;
        rect.size = CGSizeMake(rect.size.width, 50);
        footer.frame = rect;
        self.mj_footer = footer;
    }
}

- (void)listViewLoadAllData:(BOOL)isLoadAll {
    self.mj_footer.hidden = NO;
    if (isLoadAll) {
        [self layoutIfNeeded];
        if ((!self.isHiddenLoadAllDataLabel) && self.contentSize.height > self.frame.size.height) {
            [self.mj_footer endRefreshingWithNoMoreData];
        } else {
            self.mj_footer.hidden = YES;
        }
    }
}

- (id <CYRefreshLoadMoreProtocol>)cy_delegate {
    CYProxy *proxy = objc_getAssociatedObject(self, _cmd);
    return (id <CYRefreshLoadMoreProtocol>) proxy;
}

- (void)setCy_delegate:(id <CYRefreshLoadMoreProtocol>)cy_delegate {
    CYProxy *proxy = [CYProxy proxyWithTarget:cy_delegate];
    objc_setAssociatedObject(self, @selector(cy_delegate), proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isHiddenLoadAllDataLabel {
    NSNumber *number = (NSNumber *) objc_getAssociatedObject(self, &LIST_VIEW_IS_HIDDEN_LOAD_ALL_DATA_LABEL_KEY);
    return number ? [number boolValue] : NO;
}

- (void)setIsHiddenLoadAllDataLabel:(BOOL)isHiddenLoadAllDataLabel {
    objc_setAssociatedObject(self, &LIST_VIEW_IS_HIDDEN_LOAD_ALL_DATA_LABEL_KEY, [NSNumber numberWithBool:isHiddenLoadAllDataLabel], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
